package com.zygh.lz.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zygh.lz.dao.*;
import com.zygh.lz.entity.*;
import com.zygh.lz.entity.remould.PeopleType;
import com.zygh.lz.entity.remould.Vehicle;
import com.zygh.lz.util.DataTime;
import com.zygh.lz.util.DistanceUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class VehicleLocationService {

    @Resource
    private SectionMapper sectionMapper;
    @Resource
    private SignMapper signMapper;
    @Resource
    private PoliceDataMapper policeDataMapper;
    @Resource
    private EquipageMapper equipageMapper;
    @Resource
    private StaffMapper staffMapper;


    //查询车辆实时定位
    public List<Map<String, Object>> findVehicleLocation(String parameter) {
        if (StringUtils.isEmpty(parameter)) {
            return null;
        }
        List<Vehicle> vehicleList = new Gson().fromJson(parameter, new TypeToken<List<Vehicle>>() {
        }.getType());
        List<Map<String, Object>> onLineInfo = new ArrayList<>();
        if (vehicleList.size() == 0) {
            return null;
        }
        vehicleList.forEach(vehicle -> {
            if (vehicle.getSectionId() != null) {
                Section section = sectionMapper.selectByPrimaryKey(vehicle.getSectionId());
                String sectionName = section.getSectionName();
                String dui = sectionName.substring(0, sectionName.indexOf("队") + 1);
                //List<Map<String, Object>> vehicleLocation = vehicleLocationMapper.findVehicleLocation(dui, vehicle.getType());
                String type = vehicle.getType();
                if ("车".equals(type)) {
                    type = "警车";
                }
                if ("5G执法记录仪".equals(type)) {
                    type = "执法记录仪";
                }
                List<Map<String, Object>> vehicleLocation = equipageMapper.findFacilityLocaltion(dui, type);
                //区分警车带不带图传
                for (Map<String, Object> map : vehicleLocation) {
                    if (map.get("pictureType").equals("警车")) {
                        map.put("pictureType", "车");
                    }
                    if (map.get("pictureType").equals("执法记录仪")) {
                        map.put("pictureType", "5G执法记录仪");
                    }
                    if (map.get("remark2") != null) {
                        //带图传车辆
                        map.put("carFlag", 1);
                    } else {
                        //不带图传车辆
                        map.put("carFlag", 0);
                    }
                }
                DistanceUtil.merge(onLineInfo, vehicleLocation);
            } else {
                //List<Map<String, Object>> vehicleLocation1 = vehicleLocationMapper.findVehicleLocation(null, vehicle.getType());
                String type = vehicle.getType();
                if ("车".equals(type)) {
                    type = "警车";
                }
                if ("5G执法记录仪".equals(type)) {
                    type = "执法记录仪";
                }
                List<Map<String, Object>> vehicleLocation1 = equipageMapper.findFacilityLocaltion(null, type);
                for (Map<String, Object> map : vehicleLocation1) {
                    if (map.get("remark2") != null) {
                        map.put("carFlag", 1);
                    } else {
                        map.put("carFlag", 0);
                    }
                    if (map.get("pictureType").equals("警车")) {
                        map.put("pictureType", "车");
                    }
                    if (map.get("pictureType").equals("执法记录仪")) {
                        map.put("pictureType", "5G执法记录仪");
                    }
                }
                DistanceUtil.merge(onLineInfo, vehicleLocation1);
            }
        });

        return onLineInfo;
    }


    //警情在线数量
    public List<Map<String, Object>> alertLocation(String parameter) {
        List<Vehicle> vehicleList = new Gson().fromJson(parameter, new TypeToken<List<Vehicle>>() {
        }.getType());
        List<Map<String, Object>> alertInfo = new ArrayList<>();
        if (vehicleList.size() == 0) {
            return null;
        }
        vehicleList.forEach(vehicle -> {
            if (vehicle.getSectionId() != null) {
                Section section = sectionMapper.selectByPrimaryKey(vehicle.getSectionId());
                String sectionTel = section.getSectionTel();
                List<Map<String, Object>> mapList = policeDataMapper.findalertInfo(sectionTel, DataTime.getStartTheDay(), DataTime.getEndDate());
                DistanceUtil.merge(alertInfo, mapList);
            } else {
                List<Map<String, Object>> mapList = policeDataMapper.findalertInfo(null, DataTime.getStartTheDay(), DataTime.getEndDate());
                DistanceUtil.merge(alertInfo, mapList);
            }
        });

        return alertInfo;
    }

    //在线车辆数
    public Map onLineVehicleLocation(Integer id) {
        Map<String, Object> map = new HashMap<>();
        String dui = null;
        String tel = null;
        if (id != null) {
            Section section = sectionMapper.selectByPrimaryKey(id);
            String sectionName = section.getSectionName();
            dui = sectionName.substring(0, sectionName.indexOf("队") + 1);
            tel = section.getSectionTel();
        }

        List<String> policeOfficers = signMapper.findOnlineSignByType(dui, "民警");
        List<String> auxiliaryOfficers = signMapper.findOnlineSignByType(dui, "辅警");
        //车+警车
        map.put("patrolWagon", equipageMapper.findFacilityCount(dui, "警车"));
        //摩托车
        map.put("motorbike", equipageMapper.findFacilityCount(dui, "摩托车"));
        //民警
        map.put("policeOfficers", policeOfficers.size());
        //辅警
        map.put("auxiliaryOfficers", auxiliaryOfficers.size());
        //4G执法记录仪
        map.put("recorder", equipageMapper.findFacilityCount(dui, "执法记录仪"));
        //数字电台
        map.put("radio", equipageMapper.findFacilityCount(dui, "数字电台"));
        //警务通
        map.put("policeTong", equipageMapper.findFacilityCount(dui, "警务通"));
        //辅警通
        map.put("auxiliaryTong", equipageMapper.findFacilityCount(dui, "辅警通"));
        //警情坐标
        map.put("alert", policeDataMapper.selectCount(Wrappers.<PoliceData>lambdaQuery()
                .eq(StringUtils.isNotEmpty(dui), PoliceData::getCzhdw, tel)
                .between(PoliceData::getJjsj01, DataTime.getStartTheDay(), DataTime.getEndDate())));
        return map;
    }


    //累计在线设备数
    public Map onLineVehicles(Integer id) {
        Map<String, Object> map = new HashMap<>();
        String dui = null;
        String tel = null;
        if (id != null) {
            Section section = sectionMapper.selectByPrimaryKey(id);
            String sectionName = section.getSectionName();
            dui = sectionName.substring(0, sectionName.indexOf("队") + 1);

            if (section.getSectionPid() == 74 && !section.getSectionName().contains("支队")) {
                tel = null;
            } else {
                tel = section.getSectionTel();
            }
        }

        //车+警车累计在线数
        //map.put("patrolWagon", equipageMapper.findFacilityCount(dui, "车") + equipageMapper.findFacilityUp(dui, "警车"));
        map.put("patrolWagon", equipageMapper.findFacilityTotal(dui, "警车"));
        //4G执法记录仪
        map.put("recorder", equipageMapper.findFacilityUp(dui, "执法记录仪"));
        //数字电台
        map.put("radio", equipageMapper.findFacilityUp(dui, "数字电台"));
        //警务通
        map.put("policeTong", equipageMapper.findFacilityUp(dui, "警务通"));
        //辅警通
        map.put("auxiliaryTong", equipageMapper.findFacilityUp(dui, "辅警通"));
        // 摩托车
        map.put("motorcycle", equipageMapper.findFacilityTotal(dui, "摩托车"));
        //警情坐标
        map.put("alert", policeDataMapper.selectCount(Wrappers.<PoliceData>lambdaQuery()
                .eq(StringUtils.isNotEmpty(tel), PoliceData::getCzhdw, tel)
                .between(PoliceData::getJjsj01, DataTime.getStartTheDay(), DataTime.getEndDate())
                .isNotNull(PoliceData::getSfdxzb)));
        return map;
    }

    public List<Map<String, Object>> onLinePoliceStrength(String parameter) {
        if (StringUtils.isEmpty(parameter)) {
            return null;
        }
        List<PeopleType> vehicleList = new Gson().fromJson(parameter, new TypeToken<List<PeopleType>>() {
        }.getType());
        List<Map<String, Object>> onLineInfo = new ArrayList<>();
        if (vehicleList.size() == 0) {
            return null;
        }

        vehicleList.forEach(vehicle -> {
            if (vehicle.getSectionId() != null) {
                Section section = sectionMapper.selectByPrimaryKey(vehicle.getSectionId());
                String sectionName = section.getSectionName();
                String dui = sectionName.substring(0, sectionName.indexOf("队") + 1);
                String type = vehicle.getType();
                List<Map<String, Object>> mapList = signMapper.onlineSignByTypes(dui, type);
                mergeGps(mapList);
                DistanceUtil.merge(onLineInfo, mapList);

            } else {
                List<Map<String, Object>> mapList = signMapper.onlineSignByTypes(null, vehicle.getType());
                mergeGps(mapList);
                DistanceUtil.merge(onLineInfo, mapList);
            }
        });
        return onLineInfo;
    }

    @Resource
    LocationMapper locationMapper;

    private void mergeGps(List<Map<String, Object>> mapList) {
        for (Map map1 : mapList) {
//            String staffId = map1.get("staffid").toString();
//            Staff staff = staffMapper.selectByPrimaryKey(Integer.valueOf(staffId));
            //Location maps = locationMapper.findLocationEnd(Integer.valueOf(staffId));
//            Equipage maps = equipageMapper.findEquipageEnd(map1.get("warningsignal").toString());
            Location location = locationMapper.findLocationEnd(Integer.valueOf(map1.get("staffid").toString()));
            if (location != null && StringUtils.isNotEmpty(location.getLongitude())) {
                map1.put("gpsx", location.getLongitude());
                map1.put("gpsy", location.getLatitude());
            } else {
                //避免少字段无法合并
                map1.put("gpsx", "");
                map1.put("gpsy", "");
            }

        }
    }
}
